<?php
//-------------------------------------------------------------------------------------------------------
    require_once($config['app_dir'] . '/includes/db.class.php');
    //---------------------------------------------------------------------------------------------------
    $ADODB_DATE_LOCALE = array('Y-m-d','H:i:s');
    //---------------------------------------------------------------------------------------------------
    class DateConvert {

        static function SQLDate($aFormat='Y-m-d H:i:s', $aField=false) {
            return objDataBase::GetInstance()->handler->SQLDate($aFormat, $aField);
        } //SQLDate

        static function DBDate($aValue=false) { //DateConvert::DBDate(1165269600);
            return objDataBase::GetInstance()->handler->DBTimeStamp($aValue);
        } //DBDate

        static function UnixDate($aValue=false) { //DateConvert::UnixDate('2006-12-05 12:35');
            return objDataBase::GetInstance()->handler->UnixTimeStamp($aValue);
        } //UnixDate

        static function Date($aFormat, $aValue=false) {//DateConvert::Date()
            return adodb_gmdate($aFormat, $aValue);
        } //Date

        static function Date2($aFormat, $aValue=false) {//DateConvert::Date2()
            return adodb_date2($aFormat, $aValue);
        } //Date

        static function LastDay($aYear, $aMonth, $aFrmt=false) {
            $lastDay = 0;

        	for($i=28; checkdate($aMonth, $i, $aYear); $i++) $lastDay = $i;
            $dt = strtotime("$aYear-$aMonth-$lastDay 23:59:59");

            if( $aFrmt ) return DateConvert::Date($aFrmt, $dt);
                else return $dt;
        } //LastDay

        static function LastDayFromUnix($aDate, $aFrmt=false) {
            return DateConvert::LastDay(DateConvert::Date('Y', $aDate), DateConvert::Date('m', $aDate), $aFrmt );
        } //LastDayFromUnix

		static function isWeekendDay($aTimeStamp) {
            return  0 == intval( DateConvert::Date('w', $aTimeStamp) ) ||
					6 == intval( DateConvert::Date('w', $aTimeStamp) ) ;
        }//isWeekendDay


    }//class DateConvert
    //-----------------------------------------------------------------------------------------------
    class objDate {
            public $delimeter = '-';
            private $_date = false;

        public function __construct($aValue=false) {
            $this->set($aValue);
            if( $this->isEmpty() ) $this->_date = DateConvert::Date2('U');
        } //constructor

        public function isEmpty() {
            return empty($this->_date) || !$this->_date;
        } //isEmpty

        public function __get($aName) {
            $aName = strtolower($aName);
            switch( $aName ) {
                case 'date_mdy':
                    $format = 'm' . $this->delimeter . 'd' . $this->delimeter . 'Y';
                    return DateConvert::Date2($format, $this->_date);
                break;
                //--------------------------------------------------
                case 'date_dmy':
                    $format = 'd' . $this->delimeter . 'm' . $this->delimeter . 'Y';
                    return DateConvert::Date2($format, $this->_date);
                break;
                //--------------------------------------------------
                case 'date_ymd':
                    $format = 'Y' . $this->delimeter . 'm' . $this->delimeter . 'd';
                    return DateConvert::Date2($format, $this->_date);
                break;
                //--------------------------------------------------
                case 'date_rfc':
                    return DateConvert::Date2('r', $this->_date);
                break;
                //--------------------------------------------------

                case 'datetime_mdy':
                    $format = 'm' . $this->delimeter . 'd' . $this->delimeter . 'Y H:i.s';
                    return DateConvert::Date2($format, $this->_date);
                break;
                //--------------------------------------------------
            } //switch
        } //getter
/*
        public function __set($aName, $aValue){

        } //setter
*/
        public function Dump() {
            echo "Date dump: \n";
            var_dump($this->_date);
        } //Dump

        public function set($aValue) {
            if( !$aValue ) $this->_date = false;
            if( is_int($aValue) ) $this->_date = intval($aValue);
            elseif( is_string($aValue) ) {
                if( 10==strlen(trim($aValue)) ) {
                    $aValue .= ' 00:00:01';
                }
                $this->_date = DateConvert::Date2('U', $aValue);
            }
        } //set
/*
	a - "am" or "pm"
	A - "AM" or "PM"
	d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"
	D - day of the week, textual, 3 letters; e.g. "Fri"
	F - month, textual, long; e.g. "January"
	g - hour, 12-hour format without leading zeros; i.e. "1" to "12"
	G - hour, 24-hour format without leading zeros; i.e. "0" to "23"
	h - hour, 12-hour format; i.e. "01" to "12"
	H - hour, 24-hour format; i.e. "00" to "23"
	i - minutes; i.e. "00" to "59"
	j - day of the month without leading zeros; i.e. "1" to "31"
	l (lowercase 'L') - day of the week, textual, long; e.g. "Friday"
	L - boolean for whether it is a leap year; i.e. "0" or "1"
	m - month; i.e. "01" to "12"
	M - month, textual, 3 letters; e.g. "Jan"
	n - month without leading zeros; i.e. "1" to "12"
	O - Difference to Greenwich time in hours; e.g. "+0200"
	Q - Quarter, as in 1, 2, 3, 4
	r - RFC 2822 formatted date; e.g. "Thu, 21 Dec 2000 16:01:07 +0200"
	s - seconds; i.e. "00" to "59"
	S - English ordinal suffix for the day of the month, 2 characters;
	   			i.e. "st", "nd", "rd" or "th"
	t - number of days in the given month; i.e. "28" to "31"
	T - Timezone setting of this machine; e.g. "EST" or "MDT"
	U - seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
	w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)
	Y - year, 4 digits; e.g. "1999"
	y - year, 2 digits; e.g. "99"
	z - day of the year; i.e. "0" to "365"
	Z - timezone offset in seconds (i.e. "-43200" to "43200").
	   			The offset for timezones west of UTC is always negative,
				and for those east of UTC is always positive.
*/

        public function get($format=false) {
            if( $format ) return DateConvert::Date($format, $this->_date);
            else return $this->_date;
        }//get

        public function toInteger() {
            return intval($this->_date);
        } //toInteger

        public function toString($format=false) {
            if( false==$format ) $format = 'r';
            return $this->Get($format);
        } //toString

        public function Range(objDate $Start, objDate $Finish, $fStart=false, $fFinish=false) {
            $rStart = sign($this->toInteger() - $Start->toInteger()) + ( $fStart ? 1 : 0);
            $rFinish = sign($Finish->toInteger() - $this->toInteger()) + ( $fFinish ? 1 : 0);
            return ($rStart>0 && $rFinish>0);
        } //Range

    } //class Date
    //-----------------------------------------------------------------------------------------------
    class DateRoutine {

        static public function Compare(objDate $Dt1, objDate $Dt1) {
            return sign($Dt2->toInteger() - $Dt1->toInteger());
        } //compare

    } //class DateRoutine
//-------------------------------------------------------------------------------------------------------
?>
